﻿using Hospital.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using WorkflowCore.Persistence.EntityFramework.Models;
using WorkflowCore.Persistence.EntityFramework.Services;

namespace Hospital.Infrastructrue
{
    public class DataDbContext : WorkflowDbContext
    {
        private readonly IConfiguration _configuration;

        public DataDbContext(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        // 医生表
        public DbSet<Doctor> Doctors { get; set; }
        
        // 服务类型表
        public DbSet<ServiceType> ServiceTypes { get; set; }
        
        // 服务审核工作流表
        public DbSet<ServiceAuditWorkflow> ServiceAuditWorkflows { get; set; }
        
        // 服务审核步骤表
        public DbSet<ServiceAuditStep> ServiceAuditSteps { get; set; }
        
        // 粉丝表
        public DbSet<Fan> Fans { get; set; }
        public DbSet<CheckItem> CheckItems { get; set; }
        public DbSet<CheckItemType> CheckItemTypes { get; set; }
        public DbSet<HospitalBranch> HospitalBranches { get; set; }
        public DbSet<OrderRecord> OrderRecords { get; set; }

        
        public DbSet<PersistedEvent> Event { get; set; }
        public DbSet<PersistedExecutionError> ExecutionError { get; set; }
        public DbSet<PersistedExecutionPointer> ExecutionPointer { get; set; }
        public DbSet<PersistedExtensionAttribute> ExtensionAttribute { get; set; }
        public DbSet<PersistedScheduledCommand> ScheduledCommand { get; set; }
        public DbSet<PersistedSubscription> Subscription { get; set; }
        public DbSet<PersistedWorkflow> Workflow { get; set; }
        public DbSet<Fan> Fan { get; set; }
        protected override void ConfigureEventStorage(EntityTypeBuilder<PersistedEvent> builder)
        {
            builder.ToTable("Event");
        }

        protected override void ConfigureExecutionErrorStorage(EntityTypeBuilder<PersistedExecutionError> builder)
        {
            builder.ToTable("ExecutionError");
        }

        protected override void ConfigureExecutionPointerStorage(EntityTypeBuilder<PersistedExecutionPointer> builder)
        {
            builder.ToTable("ExecutionPointer");
        }

        protected override void ConfigureExetensionAttributeStorage(EntityTypeBuilder<PersistedExtensionAttribute> builder)
        {
            builder.ToTable("ExtensionAttribute");
        }

        protected override void ConfigureScheduledCommandStorage(EntityTypeBuilder<PersistedScheduledCommand> builder)
        {
            builder.ToTable("ScheduledCommand");
        }

        protected override void ConfigureSubscriptionStorage(EntityTypeBuilder<PersistedSubscription> builder)
        {
            builder.ToTable("Subscription");
        }

        protected override void ConfigureWorkflowStorage(EntityTypeBuilder<PersistedWorkflow> builder)
        {
            builder.ToTable("Workflow");
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(_configuration.GetConnectionString("DefaultConnection"), new MySqlServerVersion("5.7"));
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置Doctor表
            modelBuilder.Entity<Doctor>(entity =>
            {
                entity.ToTable("Doctors");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Gender).IsRequired().HasMaxLength(10);
                entity.Property(e => e.Phone).HasMaxLength(20);
                entity.Property(e => e.Hospital).HasMaxLength(100);
                entity.Property(e => e.Department).HasMaxLength(50);
                entity.Property(e => e.Title).HasMaxLength(50);
                entity.Property(e => e.BankCardNumber).HasMaxLength(30);
                entity.Property(e => e.Specialty).HasMaxLength(500);
                entity.Property(e => e.Introduction).HasMaxLength(1000);
                entity.Property(e => e.Certificates).HasMaxLength(500);
                entity.Property(e => e.Photo).HasMaxLength(500);
                entity.Property(e => e.AuditStatus).HasMaxLength(20);
                entity.Property(e => e.ServiceAudit).HasMaxLength(20);
                entity.Property(e => e.IsDeleted).HasDefaultValue(false);
            });

            // 配置ServiceType表
            modelBuilder.Entity<ServiceType>(entity =>
            {
                entity.ToTable("ServiceTypes");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.IsEnabled).HasDefaultValue(true);
                entity.Property(e => e.SortOrder).HasDefaultValue(0);
                entity.Property(e => e.IsDeleted).HasDefaultValue(false);
            });

            // 配置ServiceAuditWorkflow表
            modelBuilder.Entity<ServiceAuditWorkflow>(entity =>
            {
                entity.ToTable("ServiceAuditWorkflows");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.DoctorId).IsRequired();
                entity.Property(e => e.Status).IsRequired().HasMaxLength(20);
                entity.Property(e => e.CreatedTime).IsRequired();
                entity.Property(e => e.Auditor).HasMaxLength(100);
                entity.Property(e => e.Remarks).HasMaxLength(1000);
                entity.Property(e => e.IsDeleted).HasDefaultValue(false);

                // 配置与Doctor表的关系
                entity.HasOne(e => e.Doctor)
                      .WithMany(e => e.ServiceAuditWorkflows)
                      .HasForeignKey(e => e.DoctorId)
                      .OnDelete(DeleteBehavior.Cascade);
            });

            // 配置ServiceAuditStep表
            modelBuilder.Entity<ServiceAuditStep>(entity =>
            {
                entity.ToTable("ServiceAuditSteps");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.WorkflowId).IsRequired();
                entity.Property(e => e.ServiceName).IsRequired().HasMaxLength(100);
                entity.Property(e => e.AuditStatus).IsRequired().HasMaxLength(20);
                entity.Property(e => e.Remarks).HasMaxLength(500);
                entity.Property(e => e.Auditor).HasMaxLength(100);
                entity.Property(e => e.StepOrder).IsRequired();
                entity.Property(e => e.IsDeleted).HasDefaultValue(false);

                // 配置与ServiceAuditWorkflow表的关系
                entity.HasOne(e => e.Workflow)
                      .WithMany(e => e.Steps)
                      .HasForeignKey(e => e.WorkflowId)
                      .OnDelete(DeleteBehavior.Cascade);
            });

            
        }
        
    }
}
